package com.htbb.common.base.exception;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htbb.common.base.Result;


@ControllerAdvice
public class ExceptionHandler {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		r.setMsgCode(Result.FAILED);
    	r.setMsg("Unknown Exception");
        logger.error("Unknown Exception", e);
		return r;
    }
	
	@org.springframework.web.bind.annotation.ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result<Object> MissingServletRequestParameterExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		r.setMsgCode(Result.FAILED);
    	r.setMsg("Required String parameter is not present");
        logger.error("缺少必要参数", e);
		return r;
    }
	
	@org.springframework.web.bind.annotation.ExceptionHandler(ParseException.class)
    @ResponseBody
    public Result<Object> ParseExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		r.setMsgCode(Result.FAILED);
    	r.setMsg(e.getMessage());
        logger.error(e.getMessage(), e);
		return r;
    }
	
	@org.springframework.web.bind.annotation.ExceptionHandler(BizException.class)
    @ResponseBody
    public Result<Object> BizExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		String message = e.getMessage();
		r.setMsgCode(Result.FAILED);
    	r.setMsg(message);
        logger.error("service层业务系统执行发生错误:"+e.getMessage(), e);
		return r;
    }
	
	@org.springframework.web.bind.annotation.ExceptionHandler(DaoException.class)
    @ResponseBody
    public Result<Object> DaoExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		String message = e.getMessage();
		r.setMsgCode(Result.FAILED);
    	r.setMsg(message);
        logger.error("dao层数据库系统执行发生错误:"+e.getMessage(), e);
		return r;
    }
	
	@org.springframework.web.bind.annotation.ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result<Object> HttpRequestMethodNotSupportedExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<Object> r = new Result<Object>();
		String message = e.getMessage();
		r.setMsgCode(Result.FAILED);
    	r.setMsg(message);
        logger.error(e.getMessage(), e);
		return r;
    }
	
	
}
